package com.isoftstone.iot.city.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module.Feature;
import com.isoftstone.dreambase.mapper.JsonMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class SysBeanConfig {
    @Bean(name = "jsonMapper")
    public JsonMapper getJsonMapper() {
        JsonMapper jMapper = new JsonMapper();

        List<Module> modules = new ArrayList<>();
        Hibernate5Module hibernate5Module = new Hibernate5Module();
        hibernate5Module.disable(Feature.USE_TRANSIENT_ANNOTATION);
        modules.add(hibernate5Module);
        jMapper.setModules(modules);

        ObjectMapper mapper = jMapper.getMapper();
        //未知的属性不做处理
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // map 中value为null的不生成json
        mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
        mapper.configure(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS, true);
        // java bean中属性为null的 不生成json
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        return jMapper;
    }
}
